package com.rjwh.dingdong.client.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.community.CommunityPostDetailActivity;
import com.rjwh.dingdong.client.activity.webbrowser.WebActivity;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.ScoreRecordListData;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.util.UrlUtil;
import com.rjwh.dingdong.client.util.ViewHolder;
import com.rjwh.dingdong.client.widget.RoundImageView;

/**
 * @author Kay
 * @ClassName MorePointsDetialListAdapter
 * @Description
 * @date 2016-9-23
 * @history 2016-9-23 author: description:
 */
public class MorePointsDetialListAdapter extends AdapterBase<ScoreRecordListData> {

    private Context context;

    public MorePointsDetialListAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected View getExView(int position, View convertView, ViewGroup parent) throws Exception {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.more_points_detial_list_item, null);
        }
        RoundImageView imgRV = ViewHolder.get(convertView, R.id.more_points_detial_list_item_img);
        final TextView contentTv = ViewHolder.get(convertView, R.id.more_points_detial_list_item_content_tv);
        TextView timeTv = ViewHolder.get(convertView, R.id.more_points_detial_list_item_time_tv);
        TextView pointsTv = ViewHolder.get(convertView, R.id.more_points_detial_list_item_points_tv);

        ScoreRecordListData scoreRecordListData = getList().get(position);
        if (scoreRecordListData == null) {
            return convertView;
        }
        String avatar = scoreRecordListData.getAvatar();
        String title = scoreRecordListData.getTitle();
        String time = scoreRecordListData.getTime();
        String scoretype = scoreRecordListData.getScoretype();
        String scorevalue = scoreRecordListData.getScorevalue();
        final String actionurl = scoreRecordListData.getActionurl();

        Glide.with(context).load(avatar).dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.icon_pic_holder)
                .error(R.drawable.icon_pic_holder)
                .into(imgRV);

        if (!AsStrUtils.isEmpty(title)) {
            contentTv.setText(title);
        }
        if (!AsStrUtils.isEmpty(time)) {
            timeTv.setText(time);
        }
        if (!AsStrUtils.isEmpty(scoretype) && !AsStrUtils.isEmpty(scorevalue)) {
            if ("1".equals(scoretype)) {
                pointsTv.setTextColor(context.getResources().getColor(R.color.blue_button_nor));
            } else if ("2".equals(scoretype)) {
                pointsTv.setTextColor(context.getResources().getColor(R.color.red_tab));
            }
            pointsTv.setText(scorevalue);
        }
        if (!AsStrUtils.isEmpty(actionurl)) {
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Uri uri = Uri.parse(actionurl);
                    String type = uri.getQueryParameter("type");
                    if (type.equals("community")) {
                        String topicid = uri.getQueryParameter("topicid");
                        String filepath = uri.getQueryParameter("filepath");
                        String webUrl = UrlUtil.getWebUrl(context, filepath);
                        Intent intent = new Intent(context, CommunityPostDetailActivity.class);
                        intent.putExtra("topicid", topicid);
                        intent.putExtra("url", webUrl);
                        context.startActivity(intent);
                    } else if (type.equals("activity")) {
                        String filepath = uri.getQueryParameter("url");
                        String webUrl = UrlUtil.getWebUrl(context, filepath);
                        WebActivity.startActivity(context, webUrl);
                    }
                }
            });
        }

        return convertView;
    }
}
